package com.lebang.controller.api.bus;

import com.lebang.common.utils.MD5;
import com.lebang.component.RedisClient;
import com.lebang.feign.bus.BusFeign;
import com.lebang.model.RequestUserInfo;
import com.lebangweb.common.UUIDUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 公交
 *
 * @Author: hwq
 * @Date: 2020/1/8 13:52
 * @Version 1.0
 */
@Api(tags = "公交")
@RestController
@RequestMapping("/api/bus")
public class BusApi {
    @Autowired
    private BusFeign busFeign;

    @Autowired
    private RedisClient redisClient;

    /**
     * 获取热门城市列表
     * @param request
     * @return
     */
    @ApiOperation("热门城市列表")
    @GetMapping("/hot")
    public JsonData hot(HttpServletRequest request) {
        return busFeign.hot();
    }

    /**
     * 城市列表
     *
     * @return 返回结果
     */
    @ApiOperation("城市列表")
    @GetMapping("/city")
    public JsonData city(HttpServletRequest request) {
        return busFeign.city();
    }

    /**
     * 搜索公交线路
     *
     * @param request 请求数据
     * @return 返回结果
     */
    @ApiOperation("搜索公交线路")
    @GetMapping("/routes")
    public JsonData routes(HttpServletRequest request, @ApiParam(name = "城市id") @RequestParam(name = "cityId") String cityId,
                           @ApiParam(name = "公交线路") @RequestParam(name = "keywords") String keywords) {
        RequestUserInfo userInfo = new RequestUserInfo(request);
        String uuid = UUIDUtil.M();
        try{
            boolean lock = redisClient.tryGetDistributedLock("LOCK:ROUTES" + userInfo.getImei(), uuid, 3000);
            if (!lock) {
                return Callback.fail("500", "请求过于频繁");
            }
            String c = cityId.substring(cityId.length() - 1);
            String k = keywords.substring(keywords.length() - 1);
            String code = MD5.getMD5Code("bus"+c+k);
            JsonData jsonData = busFeign.routes(userInfo,cityId, keywords,code);
            return jsonData;
        }catch (Exception e){
            e.printStackTrace();
            redisClient.releaseDistributedLock("LOCK:ROUTES" + userInfo.getImei(), uuid);
            return Callback.fail("501",e.getMessage(),null);
        }




    }

    /**
     * 公交实时信息
     *
     * @param request 请求数据
     * @return 返回结果
     */
    @ApiOperation("公交实时信息")
    @GetMapping("/info")
    public JsonData info(HttpServletRequest request, @ApiParam(name = "城市id") @RequestParam(name = "cityId") String cityId,
                         @ApiParam(name = "路线id") @RequestParam(name = "rouseId") String rouseId,
                         @ApiParam(name = "路线编号") @RequestParam(name = "routeNum") String routeNum,
                         @ApiParam(name = "路线名称") @RequestParam(name = "routeName") String routeName) {
        RequestUserInfo userInfo = new RequestUserInfo(request);
        String uuid = UUIDUtil.M();
        try{
            boolean lock = redisClient.tryGetDistributedLock("LOCK:BUS:INFO" + userInfo.getImei(), uuid, 3000);
            if (!lock) {
                return Callback.fail("203", "请求过于频繁");
            }
            JsonData jsonData = busFeign.info(userInfo,cityId, rouseId, routeNum, routeName);
            return jsonData;
        }catch (Exception e){
            e.printStackTrace();
            redisClient.releaseDistributedLock("LOCK:BUS:INFO" + userInfo.getImei(), uuid);
            return Callback.fail("202",e.getMessage(),null);
        }


    }



    /**
     * 公交基础信息
     *
     * @param request 请求数据
     * @return 返回结果
     */
    @ApiOperation("公交基础信息")
    @GetMapping("/base")
    public JsonData base(HttpServletRequest request, @ApiParam(name = "城市id") @RequestParam(name = "cityId") String cityId,
                         @ApiParam(name = "路线id") @RequestParam(name = "rouseId") String rouseId,
                         @ApiParam(name = "路线编号") @RequestParam(name = "routeNum") String routeNum,
                         @ApiParam(name = "路线名称") @RequestParam(name = "routeName") String routeName) {
        RequestUserInfo userInfo = new RequestUserInfo(request);
        String uuid = UUIDUtil.M();
        try{
            boolean lock = redisClient.tryGetDistributedLock("LOCK:BASE:INFO" + userInfo.getImei(), uuid, 3000);
            if (!lock) {
                return Callback.fail("203", "请求过于频繁");
            }
            JsonData jsonData = busFeign.base(userInfo,cityId, rouseId, routeNum, routeName);
            return jsonData;
        }catch (Exception e){
            e.printStackTrace();
            redisClient.releaseDistributedLock("LOCK:BASE:INFO" + userInfo.getImei(), uuid);
            return Callback.fail("202",e.getMessage(),null);
        }

    }



    /**
     * 公交实时信息
     *
     * @param request 请求数据
     * @return 返回结果
     */
    @ApiOperation("公交实时信息")
    @GetMapping("/buses")
    public JsonData buses(HttpServletRequest request, @ApiParam(name = "城市id") @RequestParam(name = "cityId") String cityId,
                         @ApiParam(name = "路线id") @RequestParam(name = "rouseId") String rouseId,
                         @ApiParam(name = "路线编号") @RequestParam(name = "routeNum") String routeNum,
                         @ApiParam(name = "路线名称") @RequestParam(name = "routeName") String routeName) {
        RequestUserInfo userInfo = new RequestUserInfo(request);
        String uuid = UUIDUtil.M();
        try{
            boolean lock = redisClient.tryGetDistributedLock("LOCK:BUSES:INFO" + userInfo.getImei(), uuid, 3000);
            if (!lock) {
                return Callback.fail("203", "请求过于频繁");
            }
            JsonData jsonData = busFeign.buses(userInfo,cityId, rouseId, routeNum, routeName);
            return jsonData;
        }catch (Exception e){
            e.printStackTrace();
            redisClient.releaseDistributedLock("LOCK:BUSES:INFO" + userInfo.getImei(), uuid);
            return Callback.fail("202",e.getMessage(),null);
        }

    }


}
